package com.beici.vueblogapp.service.serivceImp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beici.vueblogapp.mapper.userBlogMapper;
import com.beici.vueblogapp.mapper.blogCommentsMapper;
import com.beici.vueblogapp.model.results;
import com.beici.vueblogapp.model.userBlog;
import com.beici.vueblogapp.model.blogComments;
import com.beici.vueblogapp.service.blogService;
import com.beici.vueblogapp.utils.dateUtils;
import com.beici.vueblogapp.utils.encryptionUtils;
import io.swagger.models.auth.In;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@Service
public class blogServiceImpl implements blogService {

    @Autowired
    private userBlogMapper blogService;

    @Autowired
    private blogCommentsMapper commentsService;

    @Override
    public results getBlogAll(Integer page) {
        results result = new results();
        IPage iPage = new Page(page,6);
        IPage iPage1 = blogService.selectPage(iPage, new QueryWrapper<userBlog>().orderByDesc("is_top"));
        //将置顶的放在前面
        List<userBlog> userBlogs = iPage1.getRecords();
        //查询每个文章对应的评论条数
        for (userBlog b: userBlogs) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("com_id",b.getId());
            System.out.println(b.getId());
            b.setCommentCount(commentsService.selectCount(queryWrapper));
        }
        result.setCode(200);
        result.setMessage("获取成功");
        result.setObject(userBlogs);
        return result;
    }

    @Override
    public results searchBlog(String content) {
        results result = new results();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("title",content);
        List<userBlog> userBlogs = blogService.selectList(queryWrapper);

        for (userBlog b: userBlogs) {
            QueryWrapper queryWrapper2 = new QueryWrapper();
            queryWrapper2.eq("com_id",b.getId());
            System.out.println(b.getId());
            b.setCommentCount(commentsService.selectCount(queryWrapper2));
        }
        result.setCode(200);
        result.setMessage("搜索成功");
        result.setObject(userBlogs);
        return result;
    }

    @Override
    public results getPageAll() {
        results result = new results();
        // 获取所有文章/10就是有几页了，因为10个文章为1页
        Integer count = blogService.selectCount(null);
        double f = (double) count / 6;
        Integer pageAll = encryptionUtils.pageAll(f);
        result.setMessage("获取成功");
        result.setCode(200);
        result.setObject(pageAll);
        return result;
    }

    @Override
    public results getGoodsBlog() {
        results result = new results();
        QueryWrapper<userBlog> queryWrapper  = new QueryWrapper<>();
        queryWrapper.select("id","title","banner,views_count").orderByDesc("views_count");
        List<userBlog> userBlogs = blogService.selectList(queryWrapper).subList(0,3);
        result.setMessage("获取成功");
        result.setCode(200);
        result.setObject(userBlogs);
        return result;
    }

    @Override
    public results getBlog(Integer id) {
        results result = new results();
        QueryWrapper<userBlog> queryWrapper  = new QueryWrapper<>();
        queryWrapper.select("id","title","pub_time","content");
        userBlog userBlog = blogService.selectByPrimaryKey(id);
        if (userBlog !=null){
            result.setMessage("获取成功");
            result.setCode(200);
            result.setObject(userBlog);
            return result;
        }
        result.setMessage("该文章不存在");
        result.setCode(404);
        result.setObject("");
        return result;
    }

    @Override
    public results getComments(Integer Id) {
        results result = new results();
        List<blogComments> blogComments = commentsService.queryCommentsInfo(Id);
        if (blogComments.isEmpty()){
            result.setMessage("该文章不存在");
            result.setCode(404);
            result.setObject("");
            return result;
        }
        result.setMessage("获取成功");
        result.setCode(200);
        result.setObject(blogComments);
        return result;
    }

    @Override
    public results addComment(Integer Id, String username, String content) {
        // 判断是否登录
        results result = new results();
        blogComments comment = new blogComments();
        comment.setComId(Id);
        comment.setContent(content);
        comment.setCreateTime(dateUtils.getCurrDate());
        comment.setUserName(username);
        int i = commentsService.insertSelective(comment);
        if (i>0){
            result.setMessage("已评论");
            result.setCode(200);
            result.setObject(true);
        }
        result.setCode(500);
        result.setMessage("该文章不存在");
        result.setObject("");
        return result;
    }

    @Override
    public results submitBlog(String title, String summary, String banner, String content, boolean isHot, boolean isTop) {
        results result = new results();
        userBlog b = new userBlog();
        b.setTitle(title);
        b.setSummary(summary);
        b.setBanner(banner);
        b.setContent(content);
        b.setIsHot(isHot);
        b.setIsTop(isTop);
        b.setPubTime(dateUtils.getCurrDate());
        int i = blogService.insertSelective(b);
        if (i>0){
            result.setMessage("已评论");
            result.setCode(200);
            result.setObject(true);
            return result;
        }
        result.setCode(500);
        result.setMessage("该文章不存在");
        result.setObject("");
        return result;
    }

    @Override
    public results isHotAndisTop(Integer id, boolean isHot, boolean isTop) {
        results result = new results();
        userBlog b = new userBlog();
        b.setId(id);
        b.setIsHot(isHot);
        b.setIsTop(isTop);
        int i = blogService.updateByPrimaryKeySelective(b);
        if (i>0){
            result.setMessage("已评论");
            result.setCode(200);
            result.setObject(true);
            return result;
        }
        result.setCode(500);
        result.setMessage("该文章不存在");
        result.setObject("");

        return result;
    }

    @Override
    public results del(Integer id) {
        results result = new results();
        int i = blogService.deleteByPrimaryKey(id);
        if (i>0){
            result.setMessage("已评论");
            result.setCode(200);
            result.setObject(true);
            return result;
        }
        result.setCode(500);
        result.setMessage("该文章不存在");
        result.setObject("");
        return result;
    }
}


